from typing import Optional, Dict, Union, Tuple

from model.cptem import CPTEM


class NetworkState(dict):
    def __init__(self, cptems: Optional[Dict[Union[str, Tuple[str, str]], CPTEM]] = None):
        super().__init__()
        if cptems is not None:
            self.set(cptems)



    def set(self, cptems: Dict[Union[str, Tuple[str, str]], CPTEM] ):
        for component, cptem in cptems.items():
            self[component] = {
                "state": cptem.get_current_state(),
            }
            for k, v in cptem.attr.items():
                if k == "topo" or k == "topo_info":
                    continue
                else:
                    self[component][k] = v

    def __str__(self):
        s = ""
        s += "{"
        for k, v in self.items():
            s += str(k) + ": " + "\n"
            s += "\t{\n"
            for k1, v1 in v.items():
                s += "\t" + str(k1) + " : " + str(v1) + "\n"
            s += "\t},\n"

        s += "}"
        return s



if __name__ == "__main__":
    from nptem import NPTEM
    nptem = NPTEM.mlag()
    ns = NetworkState(nptem.cptems)
    print(ns)




